<?php namespace Summer\Shop\Controllers;

use Backend\Classes\Controller;
use Summer\Shop\Models\ExpressModel;
use Summer\Shop\Models\OrderItemModel;
use Summer\Shop\Models\OrderModel;
use Input;
use Flash;
use Redirect;
use BackendMenu;
use Carbon\Carbon;
use ApplicationException;

class Order extends Controller
{
    public $implement = [
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',
        'Backend\Behaviors\RelationController',
        'Backend.Behaviors.ImportExportController'
    ];
    
    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $relationConfig = 'config_relation.yaml';
    public $importExportConfig = 'config_import_export.yaml';


    public $requiredPermissions = [
        'shop_order' 
    ];

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Summer.Shop', 'main-menu-item-shop', 'side-menu-item-order');
    }

    /**
     * 发货
     * @return mixed
     */
    public function onFahuo(){
        $data=Input::all();
        $record_id=$data['record_id'];
        $express_slug=$data['express_slug'];
        $express_no=$data['express_no'];
        $express=ExpressModel::where('slug',$express_slug)->first();
        $express_name=$express->name;
        $order=OrderModel::where('id',$record_id)->first();
        try {
            if($order->status_slug!='nosend'){
                throw new ApplicationException('记录不存在或不能操作');
            }
            $order->express_slug=$express_slug;
            $order->express_name=$express_name;
            $order->express_no=$express_no;
            $order->status_slug='noget';
            $order->deliver_at=Carbon::now();
            $order->save();
            //订单商品状态修改
            OrderItemModel::where('order_id',$order->id)->update(['status_slug'=>'noget']);
            Flash::success('操作成功!');
        }catch (ApplicationException $e) {
            Flash::error($e->getMessage());
        }
        return Redirect::refresh();
    }
    public function onListBtnPromptForm(){
        $listBtnParams=Input::all();
        $config_path=$listBtnParams['config_path']??'$/summer/hjd/config/list_prompt_btn_fields.yaml';
        $widgetConfig = $this->makeConfig($config_path);
        $widgetConfig->model=new ExpressModel();
        $listBtnPromptFormWidget=$this->makeWidget('Backend\Widgets\Form', $widgetConfig);
        $this->vars['listBtnPromptFormWidget'] = $listBtnPromptFormWidget;
        $this->vars['listBtnParams'] = $listBtnParams;
        return $this->makePartial('$/summer/hub/partials/_list_btn_prompt.htm');
    }

}
